package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.FamilyUnique;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class FamilyUniqueDao {
    private Context mContext;
    private Dao<FamilyUnique, Integer> familyDaoOpe;
    private DatabaseHelper helper;

    public FamilyUniqueDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            familyDaoOpe = helper.getDao(FamilyUnique.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(FamilyUnique familyUnique) {
        try {
            familyDaoOpe.create(familyUnique);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateFamily(FamilyUnique familyUnique) {
        try {
            return familyDaoOpe.update(familyUnique);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public FamilyUnique get(int id) {
        try {
            return familyDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * @return
     */
    public List<FamilyUnique> getAll() {
        List<FamilyUnique> familyList = new ArrayList<>();
        try {
            familyList = familyDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return familyList;
    }

    public FamilyUnique getByType(int  type) {
        List<FamilyUnique> familyList = null;
        try {
            familyList = familyDaoOpe.queryBuilder().where().eq
                    ("type", type).query();
            if (familyList != null && familyList.size() > 0) {
                return familyList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public FamilyUnique getByFamily(String familyId) {
        List<FamilyUnique> familyList = null;
        try {
            familyList = familyDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).query();
            if (familyList != null && familyList.size() > 0) {
                return familyList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public int update(FamilyUnique familyUnique) {
        try {
            return familyDaoOpe.update(familyUnique);
            //    LogHelper.i("deleFamilynumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public int delete(FamilyUnique familyUnique) {
        try {
            return familyDaoOpe.delete(familyUnique);
            //    LogHelper.i("deleFamilynumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleAll() {
        try {
            int number = familyDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<FamilyUnique> familyUniques) {
        try {
            int number = familyDaoOpe.delete(familyUniques);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void setTypeWithUnique(String fid, int type) {
        try {
            List<FamilyUnique> uniques = familyDaoOpe.queryBuilder().where().eq
                    ("familyId", fid).query();
            if (uniques != null && uniques.size() > 0) {
                FamilyUnique familyUnique = uniques.get(0);
                familyUnique.setType(type);
                updateFamily(familyUnique);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
